package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.dto.AnimalMarkDto;
import com.jiantong.work.impl.dao.AnimalMarkDao;
import com.jiantong.work.model.AnimalMark;
import com.jiantong.work.service.AnimalMarkReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 6/7/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class AnimalMarkReadServiceImpl implements AnimalMarkReadService {

    private final AnimalMarkDao animalMarkDao;

    @Autowired
    public AnimalMarkReadServiceImpl(AnimalMarkDao animalMarkDao){
        this.animalMarkDao = animalMarkDao;
    }

    @Override
    public Response<AnimalMark> findById(Long id) {

        try{
            AnimalMark animalMark = animalMarkDao.findById(id);
            if(animalMark == null){
                log.error("failed to find animal mark by id = {}",id);
                return Response.fail("failed to find animal mark");
            }
            return Response.ok(animalMark);
        }catch (Exception e){
            log.error("failed to find animal mark by id = {}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find animal mark");
        }
    }

    @Override
    public Response<Paging<AnimalMark>> pagingAnimalMak(Integer pageNo, Integer pageSize, Map<String, Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(animalMarkDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging animal mark",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging animal mark");
        }
    }

    /**
     *分页查询 用于畜禽出场时选择耳标list
     * @param pageNo 页码
     * @param pageSize 多少条
     * @param map 参数
     * @return
     */
    @Override
    public Response<Paging<AnimalMark>> pagingSelectAnimalMak(Integer pageNo, Integer pageSize, Map<String, Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(animalMarkDao.pagingSelectAnimalMak(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging animal mark",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging animal mark");
        }
    }

    /**
     * 分页查询所有散户中的耳标号信息(AnimalMarkDto)
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Response<Paging<AnimalMarkDto>> pagingAnimalMakByBreedFactoryName(Integer pageNo, Integer pageSize, Map<String , Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(animalMarkDao.pagingByBreedFactoryName(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to paging animal mark by breed factory",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging animal mark by breed factory");
        }
    }

    /**
     * 根据残缺耳标号模糊查询出一个耳标号lis
     * @param map
     * @return
     */
    @Override
    public Response<Paging<AnimalMark>> findByMark(Integer pageNo, Integer pageSize,  Map<String, Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Paging<AnimalMark> list = animalMarkDao.PagingFindByMark(pageInfo.getOffset(),pageInfo.getLimit(),map);
            if(list == null){
                log.error("failed to find animal markList by startAnimalMark = {}",map.get("startAnimalMark"));
                return Response.fail("failed to find animal markList");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find animal markList by startAnimalMark = {}",map.get("startAnimalMark"), Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find animal markList");
        }
    }

    /**
     * 默认查询排列靠前的十个状态为入库的耳标
     *
     * @param map
     * @return
     */
    @Override
    public Response<List<AnimalMark>> findByMarkIsNull(Map<String, Object> map) {
        try{
            List<AnimalMark> list = animalMarkDao.findByMarkIsNull(map);
            if(list == null){
                log.error("failed to find animal markList by breedFactoryId = {}",map.get("breedFactoryId"));
                return Response.fail("failed to find animal markList");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find animal markList by breedFactoryId = {}",map.get("breedFactoryId"), Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find animal markList");
        }
    }

    /**
     * 根据耳标号查询耳标号的具体信息
     *
     * @param markNumber 耳标号
     * @return AnimalMark
     */
    @Override
    public Response<AnimalMark> findByMarkNumber(String markNumber ,Integer markType) {
        try{
            AnimalMark animalMark = new AnimalMark();
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("markNumber",markNumber);
            if(markType != null){
                criteria.put("markType",markType);
                animalMark = animalMarkDao.findByMarkNumber(criteria);
            }else {
                animalMark = animalMarkDao.findByFullMarkNumber(markNumber);
            }
            return Response.ok(animalMark);
        }catch (Exception e){
            log.error("failed to find animal mark case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find animal mark");
        }
    }

    /**
     * 根据耳标号查询耳标号的具体信息（目前产地检疫出库的时候）
     *
     * @param markNumber 耳标号(全)
     * @return AnimalMark
     */
    @Override
    public Response<AnimalMark> findByMarkNumber(String markNumber) {
        try{
            return Response.ok(animalMarkDao.findByFullMarkNumber(markNumber));
        }catch (Exception e){
            log.error("failed to find animal mark case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find animal mark");
        }
    }
}
